<?php

namespace App\Http\Controllers\H5;

use App\Models\User;
use App\Models\Verify;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class SecurityController extends BaseController
{
    public function index()
    {
        return $this->view('h5.user.security', ['user'=>$this->user()]);
    }

    public function bind()
    {
        $phone = $this->request->input('phone');
        $code  = $this->request->input('code');
        $jypassword  = $this->request->input('jypassword','');

        $verify = Verify::where('phone', $phone)->where('code', $code)->first();
        if ($verify)
        {
            if ($this->user->mobile)
            {
                $this->user->jypassword = md5($jypassword);
                $this->user->save();
            } else {
                $user = User::where('mobile', $phone)->first();
                if ($user){
                    return ajaxError(2, trans('user.mobile has been taken'));
                } else {
                    $this->user->jypassword = md5($jypassword);
                    $this->user->mobile = $phone;
                    $this->user->save();
                    $verify->delete();
                }
            }
            return ajaxReturn();
        } else {
            return ajaxError(1, trans('user.captcha incorrect'));
        }
    }
}
